package com.sinochem.yunlian.ship.base.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinochem.yunlian.ship.base.entity.BsPort;

@Component
public class ShipLocationDataRest {
	
	private Logger LOG = LoggerFactory.getLogger(getClass());
	
	private static final String BD_URL = "http://api.map.baidu.com/geoconv/v1/";	
	private static final String BD_AK = "i8M61cYNhNPKqKeEaxzArzDjaKtXKY2m";

	@Bean
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	//GPS转百度坐标
	/**
	 * 获取船舶位置百度经纬度数据
	 * 
	 * @param ucShipLocation
	 */
	public void requestBdRestUrl(BsPort bsPort) {
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());

		String requestUrl = BD_URL + "?coords={coords}&from=1&to=5&ak={ak}";
		
		String coords = bsPort.getLon() + "," + bsPort.getLat();

		String resp = restTemplate().getForObject(requestUrl, String.class, coords, BD_AK);
		JSONObject result = JSON.parseObject(resp);
		
		int status =  result.getInteger("status");
		if(status == 0){
			if(result.getJSONArray("result")!=null && result.getJSONArray("result").size()>0)
			{
				JSONObject jsonObject = result.getJSONArray("result").getJSONObject(0);				
				bsPort.setLon(jsonObject.getString("x"));
				bsPort.setLat(jsonObject.getString("y"));
			}
		}
	}

}
